package com.lovo.stock.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.stock.bean.PageInfo;
import com.lovo.stock.bean.SupplierBean;
import com.lovo.stock.mapper.SupplierMapper;
import com.lovo.stock.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, SupplierBean> implements ISupplierService {
    @Autowired
    SupplierMapper supplierMapper;


    @Override
    public IPage<SupplierBean> querySupplierByPage(PageInfo<SupplierBean> pageInfo) {
        QueryWrapper q=new QueryWrapper<>();
        //动态判断,首先判断供应商是否为空
        if (pageInfo.getData()!=null){
            //供应商名不为空
            if (pageInfo.getData().getSupplierName()!=null&&!"".equals(pageInfo.getData().getSupplierName())){
                q.like("supplier_name",pageInfo.getData().getSupplierName());
            }
            //经营范围不为空
            if (pageInfo.getData().getSupplierNatureOfBusiness()!=null&&!"".equals(pageInfo.getData().getSupplierNatureOfBusiness())){
                q.like("supplier_natureOfBusiness",pageInfo.getData().getSupplierNatureOfBusiness());
            }
        }
        IPage<SupplierBean> page=supplierMapper.selectPage(new Page<>(pageInfo.getCurPage(), pageInfo.getPageSize()),q);
        return page;
    }

    @Override
    public List<SupplierBean> queryAllSupplier() {
        return supplierMapper.selectList(null);
    }

    @Override
    public SupplierBean querySupplierInfoById(int id) {
       return supplierMapper.selectById(id);
    }

    @Override
    public void updateSupplier(SupplierBean supplierBean) {
        supplierMapper.updateById(supplierBean);
    }

    @Override
    public void addSupplier(SupplierBean supplierBean) {
        supplierMapper.insert(supplierBean);
    }

    public void importExcel(MultipartFile file) throws Exception {

        //输入流
        InputStream inputStream = file.getInputStream();

        //importparams参数设置
        ImportParams params = new ImportParams();
        params.setTitleRows(0);
        params.setHeadRows(1);
        params.setStartRows(0);
        params.setStartSheetIndex(0);
        params.setVerifyHandler(null);

        //ExcelImportUtil工具
        List<SupplierBean> list = ExcelImportUtil.importExcel(inputStream, SupplierBean.class, params);

        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(list)) {
            log.error("导入信息不能为空");
        }

        //遍历list集合写入数据
        list.stream().forEach(supplierBean -> {
            supplierMapper.insert(supplierBean);
        });
    }

}
